<?php


namespace app\admin\controller\v1;


use app\admin\model\ArticleModel;
use app\admin\Validate\article\AddArticleValidate;
use app\admin\Validate\article\ArticleListByAdminValidate;
use app\admin\Validate\article\UpdateArticleStatusValidate;
use app\admin\Validate\article\UpdateArticleValidate;
use app\BaseController;
use app\common\Token;
use think\Db;
use think\db\BaseQuery;

class Article extends BaseController
{

    /**
     * 添加文章
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addArticle()
    {
        Token::checkToken($this->request);
        $param = (new AddArticleValidate)->validateParam();
        (new ArticleModel())->addArticle($param);
        return successJson(true, "添加成功");
    }

    /**
     * 查询文章列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getAllArticle()
    {
        Token::checkToken($this->request);
        $param = (new ArticleListByAdminValidate)->validateParam();
        $model = new ArticleModel();
        $result = $model->withPaginate(
            ['articleCategory' => function (BaseQuery $query) {
                $query->visible(['id', 'category_name']);
            }, 'articleTag' => function (BaseQuery $query) {
            }], $param, [], [], [], ['id'=>'desc']
        );
        if ($result) {
            return successJson($result, "success");
        }
        return errorJson(60004, "error");
    }

    /**
     * 删除文章
     * @param int $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deleteArticle(int $id)
    {
        Token::checkToken($this->request);
        $model = new ArticleModel();
        $model->deleteArticle($id);
        return successJson(true, "删除成功");
    }

    /**
     * 根据Id查找文章
     * @param int $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArticle(int $id)
    {
        Token::checkToken($this->request);
        $article = (new ArticleModel)->getArticleById($id, [
            'article_category'
        ]);
        if ($article) {
            return successJson($article, "成功");
        }
        return errorJson(60004, "文章不存在");
    }

    /**
     * 更新文章
     * @return \think\response\Json
     */
    public function updateArticle()
    {
        Token::checkToken($this->request);
        $param = (new UpdateArticleValidate())->validateParam();
        (new ArticleModel())->updateArticle($param);
        return successJson(true, "成功");
    }

    /**
     * 更新文章状态
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateArticleStatus(){
        Token::checkToken($this->request);
        $param = (new UpdateArticleStatusValidate())->validateParam();
        $model = ArticleModel::find($param['id']);
        $result = $model->save([
            'article_status'=>$param['status']
        ]);
        if ($result) {
            return successJson(true, "成功");
        }
        return errorJson(60003, "更新失败");
    }
}